package com.tencent.liteav.demo;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import static com.tencent.liteav.demo.livepusher.camerapush.ui.view.PusherVideoQualityFragment.SP_KEY_VIDEO_IS_FRONT;
import static com.tencent.liteav.demo.livepusher.camerapush.ui.view.PusherVideoQualityFragment.SP_KEY_VIDEO_QUALITY;
import static com.tencent.liteav.demo.livepusher.camerapush.ui.view.PusherVideoQualityFragment.SP_NAME;

public class LiveMethodActivity extends Activity implements View.OnClickListener {

    private View ll_shexiangtou_hou, ll_shexiangtou_qian;
    private View iv_shexiangtou_hou, iv_shexiangtou_qian;
    private View ll_fenbianlv1, ll_fenbianlv2, ll_fenbianlv3, ll_fenbianlv4;
    private View iv_fenbianlv1, iv_fenbianlv2, iv_fenbianlv3, iv_fenbianlv4;

    private boolean isFront = false;
    private int mQualityIndex = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_method);
        ll_shexiangtou_hou = findViewById(R.id.ll_shexiangtou_hou);
        ll_shexiangtou_qian = findViewById(R.id.ll_shexiangtou_qian);
        iv_shexiangtou_hou = findViewById(R.id.iv_shexiangtou_hou);
        iv_shexiangtou_qian = findViewById(R.id.iv_shexiangtou_qian);
        ll_fenbianlv1 = findViewById(R.id.ll_fenbianlv1);
        ll_fenbianlv2 = findViewById(R.id.ll_fenbianlv2);
        ll_fenbianlv3 = findViewById(R.id.ll_fenbianlv3);
        ll_fenbianlv4 = findViewById(R.id.ll_fenbianlv4);
        iv_fenbianlv1 = findViewById(R.id.iv_fenbianlv1);
        iv_fenbianlv2 = findViewById(R.id.iv_fenbianlv2);
        iv_fenbianlv3 = findViewById(R.id.iv_fenbianlv3);
        iv_fenbianlv4 = findViewById(R.id.iv_fenbianlv4);

        findViewById(R.id.iv_back).setOnClickListener(this);
        ll_shexiangtou_hou.setOnClickListener(this);
        ll_shexiangtou_qian.setOnClickListener(this);
        ll_fenbianlv1.setOnClickListener(this);
        ll_fenbianlv2.setOnClickListener(this);
        ll_fenbianlv3.setOnClickListener(this);
        ll_fenbianlv4.setOnClickListener(this);

        SharedPreferences s = getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        isFront = s.getBoolean(SP_KEY_VIDEO_IS_FRONT, false);
        mQualityIndex = s.getInt(SP_KEY_VIDEO_QUALITY, 1);

        if (isFront) ll_shexiangtou_qian.performClick();
        else ll_shexiangtou_hou.performClick();

        if (mQualityIndex == 0) ll_fenbianlv1.performClick();
        else if (mQualityIndex == 1) ll_fenbianlv2.performClick();
        else if (mQualityIndex == 2) ll_fenbianlv3.performClick();
        else if (mQualityIndex == 3) ll_fenbianlv4.performClick();
    }

    /**
     * 保存配置到 SharePreferences
     */
    private void saveConfigIntoSp() {
        getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
                .edit()
                .putInt(SP_KEY_VIDEO_QUALITY, mQualityIndex)
                .putBoolean(SP_KEY_VIDEO_IS_FRONT, isFront)
                .apply();
    }

    @Override
    public void onPause() {
        super.onPause();
        saveConfigIntoSp();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.ll_shexiangtou_hou:
                if (iv_shexiangtou_hou.getVisibility() == View.VISIBLE) return;
                iv_shexiangtou_hou.setVisibility(View.VISIBLE);
                iv_shexiangtou_qian.setVisibility(View.INVISIBLE);
                isFront = false;
                break;
            case R.id.ll_shexiangtou_qian:
                if (iv_shexiangtou_qian.getVisibility() == View.VISIBLE) return;
                iv_shexiangtou_hou.setVisibility(View.INVISIBLE);
                iv_shexiangtou_qian.setVisibility(View.VISIBLE);
                isFront = true;
                break;
            case R.id.ll_fenbianlv1:
                if (iv_fenbianlv1.getVisibility() == View.VISIBLE) return;
                a();
                iv_fenbianlv1.setVisibility(View.VISIBLE);
                mQualityIndex = 0;
                break;
            case R.id.ll_fenbianlv2:
                if (iv_fenbianlv2.getVisibility() == View.VISIBLE) return;
                a();
                iv_fenbianlv2.setVisibility(View.VISIBLE);
                mQualityIndex = 1;
                break;
            case R.id.ll_fenbianlv3:
                if (iv_fenbianlv3.getVisibility() == View.VISIBLE) return;
                a();
                iv_fenbianlv3.setVisibility(View.VISIBLE);
                mQualityIndex = 2;
                break;
            case R.id.ll_fenbianlv4:
                if (iv_fenbianlv4.getVisibility() == View.VISIBLE) return;
                a();
                iv_fenbianlv4.setVisibility(View.VISIBLE);
                mQualityIndex = 3;
                break;
        }
    }

    private void a() {
        iv_fenbianlv1.setVisibility(View.INVISIBLE);
        iv_fenbianlv2.setVisibility(View.INVISIBLE);
        iv_fenbianlv3.setVisibility(View.INVISIBLE);
        iv_fenbianlv4.setVisibility(View.INVISIBLE);
    }
}
